﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace PWDepositary
{
    public partial class FormAdd : Form
    {
        private bool _update;
        private int idUdate;
        public FormAdd(ListBox.ObjectCollection tags)
        {
            InitializeComponent();
            if (tags != null)
                foreach (var s in tags)
                {
                    cBoxTag.Items.Add(s);
                }
        }


        public FormAdd(string tag, ListBox.ObjectCollection tags)
        {
            InitializeComponent();
            if (tags != null)
                foreach (var s in tags)
                {
                    cBoxTag.Items.Add(s);
                }
            cBoxTag.Text = tag;
        }

        public FormAdd(int id, string name, string tag, string details, string pass, ListBox.ObjectCollection tags)
        {
            _update = true;
            idUdate = id;
            InitializeComponent();
            tBxName.Text = name;
            cBoxTag.Text = tag;
            tBxDetails.Text = details;
            tBxPW.Text = pass;
            if (tags != null)
                foreach (var s in tags)
                {
                    cBoxTag.Items.Add(s);
                }
        }

        private void menuItemCancel_Click(object sender, EventArgs e)
        {
            _update = false;
            Close();
        }

        private void menuItemOK_Click(object sender, EventArgs e)
        {
            if (_update == false)
            {
                if (tBxName.Text == "")
                {
                    lblName.ForeColor = Color.Red;
                }
                else
                {
                    lblName.ForeColor = Color.Black;
                }
                if (cBoxTag.Text=="")
                {
                    lblTag.ForeColor = Color.Red;
                }
                else
                {
                    lblTag.ForeColor = Color.Black;
                }
                if (tBxName.Text != "" && cBoxTag.Text != "")
                {
                    int id = Database.SaveToDatabase(tBxName.Text, cBoxTag.Text, tBxDetails.Text, tBxPW.Text);
                    var fm1 = Owner as MainForm;
                    fm1.AddPass(id, tBxName.Text, cBoxTag.Text, tBxDetails.Text, tBxPW.Text);
                    Close();
                }
            }
            else
            {
                var fm1 = Owner as MainForm;
                fm1.UpDateForm(idUdate, tBxName.Text, cBoxTag.Text, tBxDetails.Text, tBxPW.Text);
                _update = false;
                Close();
                Database.UpdateDatabase(idUdate, tBxName.Text, cBoxTag.Text, tBxDetails.Text, tBxPW.Text);
            }
        }

        private void FormAdd_Closed(object sender, EventArgs e)
        {
        }

        private void tBxName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                cBoxTag.Focus();
            }
        }

        private void cBoxTag_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                tBxDetails.Focus();
            }
        }

        private void tBxDetails_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                tBxPW.Focus();
            }
        }

        private void tBxPW_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                menuItemOK_Click(sender, e);
            }
        }

        private void chkBoxViewPW_CheckStateChanged(object sender, EventArgs e)
        {
            if (chkBoxViewPW.Checked)
            {
                tBxPW.PasswordChar = '\0';
            }
            else
            {
                tBxPW.PasswordChar = '*';
            }
        }
    }
}